package com.example.demo_job.controller;

import com.example.demo_job.entity.RecruitSeeker;
import com.example.demo_job.service.RecruitService;
import com.example.demo_job.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/recruit")
//@CrossOrigin(origins = {"*"},methods = {RequestMethod.GET, RequestMethod.POST})
//@CrossOrigin
public class RecruitController {
    @Autowired
    private RecruitService recruitService;

    //获取招聘人员地址：http://localhost:8080/hqdx/recruit/showRecruitSeeker
    @RequestMapping("/showRecruitSeeker")
    public R QueryRecruit(int pid){
        List<RecruitSeeker> list = recruitService.QueryRecruit(pid);
        if(list.size()==0){
            return R.error().message("查询结果为空");
        }else{
            return R.ok().data("items",list);
        }
    }

    //录取人员地址：http://localhost:8080/hqdx/recruit/QueryRecruit
    @RequestMapping("/QueryRecruit")
    public R UpdateFlag(int sid,int pid,boolean flag){
        if(!flag){
            if(!recruitService.judgeadd(pid)) return R.error().message("录用失败，已达录用人数上限");
        }
        try {
            recruitService.UpdateFlag(sid,pid,!flag);
        }catch (Exception exception){
            return R.error().message("录用失败");
        }
        recruitService.UpdatePositionNumBypid(pid,flag);
        if(flag) return R.ok().message("取消录用成功");
        return R.ok().message("录用成功");
    }


    //筛选人员地址：http://localhost:8080/hqdx/recruit/SelectByEdu
    @RequestMapping("/SelectByEdu")
    public R QueryRecruitByEdu(int pid,String sedu){
        int sedu1=0;
        switch (sedu) {
            case "小学":
                sedu1 =1;
                break;
            case "初中":
                sedu1 =2;
                break;
            case "高中":
                sedu1 =3;
                break;
            case "大学":
                sedu1 =4;
                break;
            case "研究生":
                sedu1 =5;
                break;
            case "博士":
                sedu1 =6;
                break;
            case "全部" :
                sedu1 =0;
                break;
        }
        List<RecruitSeeker> list = recruitService.QueryRecruitByEdu(pid,sedu1);
        if(list.size()==0){
            return R.error().message("查询结果为空");
        }else{
            return R.ok().data("items",list);
        }
    }
}
